package com.kedacom.report.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.context.SecurityContext;

import com.fr.file.ClusterConfigManager;
import com.fr.file.ClusterService;
import com.fr.fs.base.entity.UserInfo;
import com.fr.fs.web.service.AbstractFSAuthService;
import com.fr.general.ComparatorUtils;
import com.fr.general.VT4FR;
import com.fr.stable.StableUtils;
import com.fr.web.cluster.ClusterManager;
import com.fr.web.utils.WebUtils;
import com.kedacom.security.model.SysUser;


public class FRReportSSOFilter implements Filter {


	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		 HttpServletResponse res = (HttpServletResponse) response;
		 HttpServletRequest req = (HttpServletRequest) request;
		 SecurityContext sc = (SecurityContext) req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		 SysUser su = null;
		 if(sc != null && sc.getAuthentication() != null && sc.getAuthentication().getPrincipal() != null)
		 {
			 su = (SysUser) sc.getAuthentication().getPrincipal();
		 }
		 
		 
		Object fruser =  req.getSession().getAttribute("fr_username");
		String fruserName = "";
		if(fruser != null)
			fruserName = fruser.toString();
		if(su != null &&  "".equals(fruserName))
		{
			try {
				loginFineReport(req,res,su.getAccount());
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServletException(e);
			}
		}
			chain.doFilter(request, response);
	}	private void loginFineReport(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse
    		,String username) throws Exception
    {
    	
//    	fr_authority=SUPERROLE
//    			fr_current_privilege_loader=com.fr.fs.privilege.base.FServicePrivilegeLoader@11c635a
//    			fr_username=admin
//    			fr_fs_auth_key=com.fr.fs.privilege.entity.DaoFSAuthentication@1046303: Principal: admin; Password: [PROTECTED]; Authenticated: true; Granted Authorities: SUPERROLE: User: admin; 

    	
    	
//		String pwd  = ((SynUserReportService) ApplicationUtil.getBean(SynUserReportService.class)).queryPassword(username.trim());
//		if(frtuser == null)
//			throw new Exception("未找到用户：" +username);
    	HttpSession localHttpSession = paramHttpServletRequest.getSession();
        boolean bool = ComparatorUtils.equals(Boolean.valueOf(true), localHttpSession.getAttribute("isTemplate"));
        if (dealLoginInfo(paramHttpServletRequest, paramHttpServletResponse, "admin", "cht123456", bool))//"kedacomit1234"
        {
          UserInfo localUserInfo = new UserInfo(username, "1", Boolean.valueOf(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "fr_remember")).booleanValue());
          localUserInfo.dealBrowserCookies(paramHttpServletResponse);
          Object localObject = localHttpSession.getAttribute(bool ? "fr_template_originalUrl" : "originalURL");
          String str3 = "";
          if(localObject != null)
          {
        	  str3 = localObject.toString() + "&_=" + System.currentTimeMillis();
          }
          else
          {
        	  str3 = "&_=" + System.currentTimeMillis();
          }
          if (ClusterConfigManager.getInstance().isUseCluster())
          {
            ClusterService localClusterService = ClusterManager.getInstance().getSelfService();
            String str4 = localClusterService.getServiceName();
            str3 = str3 + "&serverID=" + str4 + "_" + System.currentTimeMillis();
          }
        }
         
    }
    protected boolean dealLoginInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2, boolean paramBoolean)
    	    throws Exception
    	  {
    	    int i = (VT4FR.isLicAvailable(StableUtils.getBytes())) && (VT4FR.FS_BI.support()) ? 1 : 0;
    	    if (i == 0)
    	      return AbstractFSAuthService.dealLoginInfo(paramHttpServletRequest, paramHttpServletResponse, paramString1, paramString2, true);
    	    return AbstractFSAuthService.dealLoginInfo(paramHttpServletRequest, paramHttpServletResponse, paramString1, paramString2, paramBoolean);
    	  }

	@Override
	public void destroy() {
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}


}
